
package com.idsmanager.zdh.service.business;


import com.idsmanager.commons.utils.PasswordHandler;
import com.idsmanager.commons.web.context.BeanProvider;
import com.idsmanager.oauth.domain.security.UserRepository;
import com.idsmanager.oauth.domain.security.Privilege;
import com.idsmanager.oauth.domain.security.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 2015/12/21
 * <p/>
 * <p/>
 * When the system firstly startup, checking and initialed default-user.
 *
 * @author Shengzhao Li
 */
public class DefaultUserInitializer {


    private static final Logger LOG = LoggerFactory.getLogger(DefaultUserInitializer.class);

    private transient UserRepository userRepository = BeanProvider.getBean(UserRepository.class);

    public DefaultUserInitializer() {
    }

    public String initial() {

        long count = userRepository.countUsers();
//        if (count > 0) {
//            return "404";
//        }

        //初始化时的账号
        String username = "zhangdh";
        //初始化时的密码
        String password = PasswordHandler.encryptPassword("zhangdh", username);
        User user = new User(username, password);
        user.privileges().addAll(Privilege.availablePrivileges());

        userRepository.saveUser(user);
//        init();
        return user.username();
    }

    private void init() {
        for(int i=0;i<20;i++){
            //初始化时的账号
            String username = "zhangdh"+i;
            //初始化时的密码
            String password = PasswordHandler.encryptPassword("zhangdh", username);
            User user = new User(username, password);
            user.privileges().addAll(Privilege.availablePrivileges());

            userRepository.saveUser(user);
        }
    }

}
